/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.ClassesAplicInterface;
import br.gov.cemaden.library.model.smp.ClassesAplic;
import br.gov.cemaden.library.model.smp.ExecAplic;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class ClassesAplicDao extends GenericDao implements ClassesAplicInterface {

    private Session session;

    public ClassesAplicDao(Session session) {
        this.session = session;
    }

    public List<ClassesAplic> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ClassesAplic> classesAplic = null;
        try {
            Query query = session.getNamedQuery("ClassesAplic.findAll");
            classesAplic = (List<ClassesAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ClassesAplicFacade.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return classesAplic;
    }

    public ClassesAplic findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ClassesAplic classesAplic = null;
        try {
            Query query = session.getNamedQuery("ClassesAplic.findById").setParameter("id", id);
            classesAplic = (ClassesAplic) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ClassesAplicFacade.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return classesAplic;
    }

    public List<ClassesAplic> findByTotal(Double total) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ClassesAplic> classesAplic = null;
        try {
            Query query = session.getNamedQuery("ClassesAplic.findByTotal").setParameter("total", total);
            classesAplic = (List<ClassesAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ClassesAplicFacade.findByTotal: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return classesAplic;
    }

    public List<ClassesAplic> findByLoaded(Double loaded) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ClassesAplic> classesAplic = null;
        try {
            Query query = session.getNamedQuery("ClassesAplic.findByLoaded").setParameter("loaded", loaded);
            classesAplic = (List<ClassesAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ClassesAplicFacade.findByLoaded: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return classesAplic;
    }

    public List<ClassesAplic> findByUnLoaded(Double unLoaded) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ClassesAplic> classesAplic = null;
        try {
            Query query = session.getNamedQuery("ClassesAplic.findByUnLoaded").setParameter("unLoaded", unLoaded);
            classesAplic = (List<ClassesAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ClassesAplicFacade.findByUnLoaded: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return classesAplic;
    }

    public List<ClassesAplic> findByExecAplic(ExecAplic execAplic) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ClassesAplic> classesAplic = null;
        try {
            Query query = session.getNamedQuery("ClassesAplic.findByExecAplic").setParameter("execAplic", execAplic);
            classesAplic = (List<ClassesAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ClassesAplicFacade.findByExecAplic: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return classesAplic;
    }

    public ClassesAplic insert(ClassesAplic classesAplic) {
        try {
            saveOrUpdate(this.session, classesAplic);
        } catch (Exception ex) {
            Logger.getLogger(ClassesAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return classesAplic;
    }

    public ClassesAplic update(ClassesAplic classesAplic) {
        try {
            saveOrUpdate(this.session, classesAplic);
        } catch (Exception ex) {
            Logger.getLogger(ClassesAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return classesAplic;
    }

    public void delete(ClassesAplic classesAplic) {
        try {
            remove(this.session, classesAplic);
        } catch (Exception ex) {
            Logger.getLogger(ClassesAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
